package com.wiegand.content.background {
	import com.wiegand.Configuration;
	import com.wiegand.loader.SwfLoader;
	import com.wiegand.util.BackgroundPattern;
	import com.wiegand.util.ColorRectangle;

	import flash.display.MovieClip;
	import flash.display.Sprite;

	/**
	 * @author gandy
	 */
	public class Background extends Sprite {

		private var _background : Sprite;
		private var _wall : Sprite;
		private var _floor : Sprite;
		private var _wallBg : BackgroundPattern;
		private var _floorBg : BackgroundPattern;
		private var _man : MovieClip;
		private var _linesManager : LinesManager;
		private var _wallLinesHolder : Sprite;
		private var _wallLinesMask : Sprite;
		private var _floorLinesHolder : Sprite;

		public function Background() {
		}
		
		public function init() : void {
			_background = ColorRectangle.create(0xf3f3f3,1,stage.stageWidth,stage.stageHeight);
			addChild(_background);
			
			_wall = ColorRectangle.create(0xf3f3f3,1,stage.stageWidth,Math.round(stage.stageHeight*0.75));
			var wallPatter : Sprite = SwfLoader.getInstance().getDisplayObject("WallPatter") as Sprite;
			_wallBg = new BackgroundPattern( _wall, wallPatter );
				
			_man = MovieClip(SwfLoader.getInstance().getDisplayObject("man_animation"));
			_man.x = stage.stageWidth - _man.width/2 - Configuration.BORDER;
			_man.y = _wall.height - _man.height/2 - 3;
			
			_wallLinesHolder = new Sprite();
			_wallLinesMask = ColorRectangle.create(0xff0000,1,_wall.width,_wall.height);
				
						
			_floor = ColorRectangle.create(0xf3f3f3,1,_wall.width*2,_wall.height*2);
			_floor.y = _wall.height;
			_floor.x = -_wall.width/2;
			var floorPatter : Sprite = SwfLoader.getInstance().getDisplayObject("FloorPatter") as Sprite;
			_floorBg = new BackgroundPattern( _floor, floorPatter );
			_floorBg.y = _floor.y;
			_floorBg.x = - _wall.width/2;
			_floorBg.z = _wall.height/2;
			_floorBg.rotationX = -80;
			
			_floorLinesHolder = new Sprite();
			_floorLinesHolder.x = _floorBg.x;
			_floorLinesHolder.y = _floorBg.y;
			_floorLinesHolder.z = _floorBg.z;
			_floorLinesHolder.rotationX = _floorBg.rotationX;
//			_floorLinesMask = ColorRectangle.create(0xff0000,.6,_floor.width,_floor.height);
//			_floorLinesMask.y = _floor.y;
//			_floorLinesMask.rotationX = _floorBg.rotationX;
					
			addChild(_floor);
			addChild(_floorBg);
					
			addChild(_floorLinesHolder);	
//			_floorLinesHolder.mask = _floorLinesMask;		
//			addChild(_floorLinesMask);	
			
			
//			var pp : PerspectiveProjection = new PerspectiveProjection();
//			pp.fieldOfView = 45;
//			pp.projectionCenter = new Point( stage.stageWidth/2, stage.stageHeight/2 );
//			pp.focalLength = 150;
//			
//			_floor.transform.perspectiveProjection = pp;
//			_floorBg.transform.perspectiveProjection = pp;
//			_floorLinesHolder.transform.perspectiveProjection = pp;
			
			addChild(_wall);
			addChild(_wallBg);	
			addChild(_man);
			addChild(_wallLinesHolder);	
			_wallLinesHolder.mask = _wallLinesMask;
			addChild(_wallLinesMask);
			
			_wallLinesHolder.addChild( SwfLoader.getInstance().getDisplayObject("WallMat") );
			_wallLinesHolder.alpha=.6;			
						
//			_linesManager = new LinesManager(_wallLinesHolder, _floorLinesHolder);
		}

		public function resize():void {	
			_background.width = stage.stageWidth;
			_background.height = stage.stageHeight;

			_wall.width = stage.stageWidth;
			_wall.height = Math.round(stage.stageHeight*0.75);
			_wallBg.resizeHandler();
			
			_wallLinesMask.height = _wall.height;
			_wallLinesMask.width = _wall.width;			
			
			_floor.width = _wall.width*2;
			_floor.height = _wall.height*2;
			_floor.y = _wall.height;
			_floor.x = -_wall.width/2;
			_floorBg.y = _floor.y;
			_floorBg.x = - _wall.width/2;
			_floorBg.z = _wall.height/2;
			
//			_floorLinesHolder.x = _floorBg.x;
			_floorLinesHolder.y = _floorBg.y;
			_floorLinesHolder.z = _floorBg.z;		
	
			_floorBg.resizeHandler();	
		
//			_floorLinesMask.height = _floor.height;
//			_floorLinesMask.width = _floor.width;
//			_floorLinesMask.y = _floor.y;

			_man.x = stage.stageWidth - _man.width/2 - Configuration.BORDER;
			_man.y = _wall.height - _man.height/2 - 3;			
		}
		
		public function updateLines(xml:XMLList):void{
//			_linesManager.createLines(xml);
		}
	}
}
